package com.dc.web.admin;

import com.dc.po.Blog;
import com.dc.po.Tag;
import com.dc.po.User;
import com.dc.service.BlogService;
import com.dc.service.CommentService;
import com.dc.service.TagService;
import com.dc.service.TypeService;
import com.dc.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    @Autowired
    private CommentService commentService;

    //博客管理列表首页
    @GetMapping("/blogs")
    public String blogs(@PageableDefault(size = 6, sort = {"updateTime"}, direction = Sort.Direction.DESC)
                                Pageable pageable,
                        BlogQuery blog,
                        Model model) {
        model.addAttribute("types", typeService.listType());
        model.addAttribute("page", blogService.listBlog(pageable, blog));
        return "admin/blogs";
    }

    //博客管理条件查询列表
    @PostMapping("/blogs/search")
    public String search(@PageableDefault(size = 6, sort = {"updateTime"}, direction = Sort.Direction.DESC)
                                 Pageable pageable, BlogQuery blog, Model model) {
        model.addAttribute("page", blogService.listBlog(pageable, blog));
        return "admin/blogs :: blogList";
    }

    //博客发布页  新增
    @GetMapping("/blogs/input")
    public String input(Model model) {
        getTypesAndTags(model);
        model.addAttribute("blog", new Blog());
        return "admin/blogs-input";
    }

    public void getTypesAndTags(Model model) {
        model.addAttribute("types", typeService.listType());
        model.addAttribute("tags", tagService.listTag());
    }

    //博客编辑页  修改
    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable Long id,
                            Model model) {
        getTypesAndTags(model);
        Blog blog = blogService.getBlog(id);
        blog.init();//初始化tagIds，将标签类集合转化得到String数组id “1,2,3” String类型
        model.addAttribute("blog", blog);
        return "admin/blogs-input";
    }


    //新增博客或更新博客
    @PostMapping("/blogs")
    public String post(Blog blog,
                       RedirectAttributes attributes,
                       HttpSession session) {
        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));//前端给的分类信息只有id，你得自己查出实体赋值剩余没有给值的属性
        blog.setTags(tagService.listTag(blog.getTagIds()));//前端给的标签信息是一组IdString "1,2,3",有新标签时“1,2,3,redis”
        Long id = blog.getId();//判断是否为新增用
        //dao层保存或更新
        Blog b = blogService.saveBlog(blog);
        if (b == null) {
            attributes.addFlashAttribute("msg", "操作失败");
        } else {
            if (id == null) {
                attributes.addFlashAttribute("msg", "新增成功");
            } else {
                attributes.addFlashAttribute("msg", "修改成功");
            }
        }
        return "redirect:/admin/blogs";
    }

    //删除博客
    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id,
                         RedirectAttributes attributes) {
        Blog blog = blogService.getBlog(id);
        if(blog.getComments().size()>0){
            commentService.deleteByBlogId(id);
            System.out.println("==============删除Comments=============================");
        }
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/admin/blogs";
    }

}
